/*
 * Factorial.java
 *
 */

package da522A_lab4A_2011;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;


/**
 *
 * @author Saeid Kazemi
 * Kristianstad University
 * February 2010
 *
 */

public class Factorial {
    
    //declarations
    Scanner myScanner;
    
    
    /**
     * Creates a new instance of Factorial
     */
    public Factorial() {
        //initializations
        myScanner = new Scanner(System.in);
    }//End of constructor
    
    public void myMain(){
        Long input;
        Long temp;
        printMenu();
        input = myScanner.nextLong();
        temp = fact1(input);
        System.out.println("Factorial " + input + " using iteration is " + temp);
        
        temp = fact2(input);
        System.out.println("Factorial " + input + " using recurive method is " + temp);
    } //End of myMain method
    
    private long fact1(long inputNumber){ //Non recursive method
        Long temp = inputNumber;
        if(temp==0)
            return 1;
        while(--inputNumber>0){
            temp *= inputNumber;
        }
        
        //The following code is doing the same as the above while loop
        //It is good to get used to read and understand the code above as fast as the code below
        /*
         inputNumber = inputNumber -1;
          while(inputNumber>0){
            temp = temp * inputNumber;
            inputNumber = inputNumber -1;
        }
         */
        return temp;
        
    } //End of fact1 method
    
    private long fact2(long inputNumber){// recursive method
        
        //implement here your code using recursion method
        if ( inputNumber <= 0){
            return 1;
        }else {
            return inputNumber*fact2(inputNumber - 1);
        }
        
    } //End of fact2 method
    
    
    public static void main(String[] args) {
        //Using an object of Factorial class
        //will help to get rid of the static problem of main method
        Factorial f1 = new Factorial();
        f1.myMain();
         System.out.println("I am in the main method of Factorial class and I expect to have fun!");
    }// End of main menu
    
    
    private  void printMenu() {
        
        System.out.println("\n**** Main menu ****");
        System.out.println("This application calculates the factorial");
        System.out.println("Enter a number");
        
    } //End of printMenu method
}


